//
//  HDProfileViewController.swift
//  Excited
//
//  Created by X140Yu on 6/13/16.
//  Copyright © 2016 X140Yu. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import SnapKit
import MJRefresh
import PKHUD

class HDProfileViewController: UITableViewController {
    
    let disposBag = DisposeBag()
//    var credit: HDCredit?
    var isLogin: Bool {
        get {
            if HDCache.sharedInstance.currentLoginUser() != nil {
                return true
            }
            return false
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(userDidLogin), name: HDUserDidLoginNotificationKey, object: nil)
        
        tableView.registerNib(UINib(nibName: "HDProfileUnLoginTableViewCell", bundle: nil), forCellReuseIdentifier: "unlogin")
        tableView.registerNib(UINib(nibName: "HDProfileLoginTableViewCell", bundle: nil), forCellReuseIdentifier: "login")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "设置", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(showSettingsVC))
        
        tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            HDAPIService.sharedInstance.getRealName { [unowned self] (realName, error) in
                dispatch_async(dispatch_get_main_queue(), {
                    if let _ = realName {
                        self.tableView.reloadData()
                    } else {
                        // TODO: error
                    }
                    self.tableView.mj_header.endRefreshing()
                })
            }

            HDAPIService.sharedInstance.getPYFA { (pyfa, error) in
                dispatch_async(dispatch_get_main_queue(), {
                    if let _ = pyfa {
                        self.tableView.reloadData()
                    } else {
                        // TODO: error
                    }
                    self.tableView.mj_header.endRefreshing()
                })
            }
        })
    }
    
    func userDidLogin() {
        self.tableView.mj_header.beginRefreshing()
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func showSettingsVC() {
        let settingsVC = UIStoryboard(name: "HDSettingsViewController", bundle: nil).instantiateInitialViewController() as! HDSettingsTableViewController
        
        navigationController?.pushViewController(settingsVC, animated: true)
    }
}

extension HDProfileViewController {
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if isLogin == false {
            let cell = tableView.dequeueReusableCellWithIdentifier("unlogin", forIndexPath: indexPath) as! HDProfileUnLoginTableViewCell
            cell.selectionStyle = .None
            return cell
        }

        let cell = tableView.dequeueReusableCellWithIdentifier("login", forIndexPath: indexPath) as! HDProfileLoginTableViewCell
        cell.selectionStyle = .None
        cell.setUserInfo()
        cell.setCreditInfo()
        return cell
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if isLogin == false {
            return 450
        }
        return 410
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
}